package com.imufe.chemicalenterprisedangerwarning.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imufe.chemicalenterprisedangerwarning.entity.po.EnterprisePO;
import com.imufe.chemicalenterprisedangerwarning.entity.po.EnterpriseTypePO;
import com.imufe.chemicalenterprisedangerwarning.entity.po.IndustryPO;
import com.imufe.chemicalenterprisedangerwarning.entity.query.EnterpriseQuery;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.*;
import com.imufe.chemicalenterprisedangerwarning.mapper.EnterpriseMapper;
import com.imufe.chemicalenterprisedangerwarning.mapper.EnterpriseTypeMapper;
import com.imufe.chemicalenterprisedangerwarning.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imufe.chemicalenterprisedangerwarning.util.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author BaiBan
 * @since 2022-09-24
 */
@Service
public class EnterpriseServiceImpl extends ServiceImpl<EnterpriseMapper, EnterprisePO> implements EnterpriseService {

    @Autowired
    EnterpriseMapper mapper;

    @Autowired
    SafetySupervisionBureauService safetySupervisionBureauService;

    @Autowired
    RegionService regionService;

    @Autowired
    EnterpriseDepartmentService enterpriseDepartmentService;

    @Autowired
    ProjectService projectService;

    @Autowired
    IndustryService industryService;

    @Autowired
    EnterpriseTypeService enterpriseTypeService;

    @Override
    public EnterpriseVO basic(Long id) {
        EnterprisePO po = mapper.selectById(id);
        return BeanUtils.copyPOJO(po, EnterpriseVO.class);
    }

    @Override
    public IPage<EnterpriseVO> page(Long current) {
        Page<EnterpriseVO> page = new Page<>(current, 10);
        mapper.page(page, null);
        return page;
    }

    @Override
    public EnterpriseDetailedVO detailed(Long id) {
        EnterprisePO po = mapper.selectById(id);
        EnterpriseDetailedVO detailed = BeanUtils.copyPOJO(po, EnterpriseDetailedVO.class);

        // 添加安监局详细
        SafetySupervisionBureauDetailedVO safetySupervisionBureauDetailedVO = safetySupervisionBureauService.detailed(po.getSafetySupervisionBureauId());
        detailed.setSafetySupervisionBureau(safetySupervisionBureauDetailedVO);

        // 具体地址
        RegionDetailedVO regionDetailedVO = regionService.detailed(po.getRegionId());
        detailed.setRegion(regionDetailedVO);

        // 行业类别
        IndustryPO industryPO = industryService.getById(po.getIndustryId());
        detailed.setIndustry(BeanUtils.copyPOJO(industryPO, IndustryVO.class));

        // 企业类型
        EnterpriseTypePO enterpriseTypePO = enterpriseTypeService.getById(po.getEnterpriseTypeId());
        detailed.setEnterpriseType(BeanUtils.copyPOJO(enterpriseTypePO, EnterpriseTypeVO.class));

        // 员工信息
        List<EnterpriseDepartmentVO> departments = enterpriseDepartmentService.list(id);
        detailed.setDepartmentList(departments);

        // 员工总人数
        Integer headcount = 0;
        for (EnterpriseDepartmentVO it : departments) {
            headcount += it.getNumber();
        }
        detailed.setHeadcount(headcount);

        // 项目信息
        List<ProjectDetailedVO> projectList = projectService.projectList(po.getId());
        detailed.setProjectList(projectList);

        return detailed;
    }

    @Override
    public EnterpriseDetailedVO create(EnterpriseQuery query) {
        EnterprisePO po = BeanUtils.copyPOJO(query, EnterprisePO.class);
        mapper.insert(po);
        return this.detailed(po.getId());
    }

    @Override
    public EnterpriseDetailedVO update(EnterpriseQuery query) {
        EnterprisePO po = BeanUtils.copyPOJO(query, EnterprisePO.class);
        mapper.updateById(po);
        return this.detailed(po.getId());
    }
}
